package com.zzy.database.controller;

import com.zzy.database.pojo.Result;
import com.zzy.database.pojo.transaction;
import com.zzy.database.service.TransactionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.pulsar.PulsarProperties;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/transactions")
public class TransactionController {

    private Result<TransactionService> result;
    @Autowired
    private TransactionService transactionService;

    @PutMapping("/deposit")
    public Result<TransactionService> deposit(@RequestBody transaction transaction) {
        if(transactionService.deposit(transaction.getAccount_id(), transaction.getAmount()))
        {
            return result.success();
        }
        else {
            return result.error("存款失败");
        }
    }

    @PutMapping("/withdraw")
    public Result<TransactionService> withdraw(@RequestBody transaction transaction) {
        if(transactionService.withdraw(transaction.getAccount_id(), transaction.getAmount()))
        {
            return result.success();
        }
        else {
            return result.error("存款失败");
        }
    }

    @PutMapping("/transfer")
    public Result transfer(@RequestBody transaction transaction) {
        if (transactionService.transfer(transaction.getAccount_id(), transaction.getDest_account_id(), transaction.getAmount()))
        {
            return result.success();
        }
        else {
            return result.error("存款失败");
        }
    }
}
